<?php
namespace app\admin\model;
use framework\ext\Util;
class themes extends \app\base\model\BaseModel{

    //模板目录路径查找
    //path:目录路径,root:锁定目录
    public function DirPath($path = null,$root = 'themes'){
        $rootpath = ROOT_PATH.$root.'\\';  //根目录
        if($path){
            $path = str_replace('\\','/',$path);
            $path = str_replace('../','',$path);
            $nowpath = realpath($rootpath.$path);
            if($nowpath){
                return $nowpath.DIRECTORY_SEPARATOR;
            }
        }
        return $rootpath;
    }
    //查找包含目录
    public function folder($path = null){
        $fileArray = NULL; 
        if(false != ($handle = opendir($path))) { 
            $i = 0; 
            while(false !== ($file = readdir ($handle))){ 
                if ($file != "." && $file != ".."){ 
                    if (is_dir($path.$file)){ 
                        $fileArray [$i] = $file; 
                    } 
                    $i ++;               
                } 
            } 
            closedir ($handle); 
        } 
        return $fileArray;
    }

    //查找文件
    //$path:更目录 $exts:*.{php,css,js,txt}
    public function files($path,$exts){
        $str = strlen($path);
        $arrdir = glob($path.$exts,GLOB_BRACE);
        if(is_array($arrdir)){
            $tpl = array();
            foreach ($arrdir as $value) {
                $tpl[] = substr($value,$str);
            }
        }
        return $tpl;
    } 

    //获取缓存列表
    public function cache_List($folder){
        return dir_size(DATA_PATH.'cache/'.$folder)%1024;
    }

    //清空指定缓存
    public function delete_cache($types) {
        if($types ==1){
            $folder = 'tpl';
        }else{
            $folder = 'log';
        }
        $file = DATA_PATH.'cache/'.$folder;
        if(is_dir($file)){
            Util::delDir($file);
        }elseif(is_file($file)){
            unlink($file);
        }else{
            return FLASE;
        }
        return true;
    }
}